package com.lagou;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import java.lang.reflect.Type;
import java.util.*;

/**
 * @author ADMIN
 * @date 2021/3/22 22:09
 */
public class TestDemo {
    public static String kSort(Map<String, Object> map) {
        ArrayList<String> stringList = new ArrayList<>();
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            stringList.add(entry.getKey());
        }
        Collections.sort(stringList);
        String base = "";
        for (String s : stringList) {
            base = base + s + "=" + map.get(s) + "&";
        }
        return base.substring(0, base.length() - 1);
//        return base;
    }

    public static String buildSig(Map<String, Object> map, String token) {
        String s =  token+"&"+kSort(map);
        String md5Hex = DigestUtils.md5Hex(s);
        byte[] bytes = Base64.encodeBase64(md5Hex.getBytes());
        String s1 = new String(bytes);
        return s1;
    }

    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();

//        map.put("main_tasktype_key", "global_face_appeal_ec");
        String st = "[{\"field\":\"additionalPhotoUrl1\",\"value\":\"62706475-9dbb-4161-b590-b1880ef53fc6|face2img|0\"},{\"field\":\"country_code\",\"value\":\"EC\"},{\"field\":\"ID_number\",\"value\":\"105\"},{\"field\":\"driverLicensePhotoUrl1\",\"value\":\"78caafe3-6a94-41f1-84c6-ae27462de09c|face2img|0\"},{\"field\":\"bareHeadPhotoUrl\",\"value\":\"d151e378-7a41-4847-9215-0fb79d98d6a2|face2img|0\"},{\"field\":\"appealId\",\"value\":\"6bfd1afe-3408-4e16-af8e-b6e647747111\"}";
        Object parse = JSONObject.parseArray(st, Type.class);


        System.out.println(parse);


//        map.put("data", parse);
//        map.put("data","{\"field\":\"additionalPhotoUrl1\",\"value\":\"62706475-9dbb-4161-b590-b1880ef53fc6|face2img|0\"},{\"field\":\"country_code\",\"value\":\"EC\"},{\"field\":\"ID_number\",\"value\":\"105\"},{\"field\":\"driverLicensePhotoUrl1\",\"value\":\"78caafe3-6a94-41f1-84c6-ae27462de09c|face2img|0\"},{\"field\":\"bareHeadPhotoUrl\",\"value\":\"d151e378-7a41-4847-9215-0fb79d98d6a2|face2img|0\"},{\"field\":\"appealId\",\"value\":\"6bfd1afe-3408-4e16-af8e-b6e647747111\"}")
//
//        map.put("access_id", 1380);
//        map.put("data", "abc");
//        String s = kSort(map);
//        String s1 = buildSig(map, "Dd#4r8noopmis?_7rfG2015@");
//        System.out.println(s1);
//        String str = "你好";
//        byte[] bytes = str.getBytes();
//        String s = new String(bytes);
//        System.out.println(s);

    }
}
